//package com.example.yunpicturebackend.aop;
//
//import com.example.yunpicturebackend.annotation.AuthCheck;
//import com.example.yunpicturebackend.exception.BusinessException;
//import com.example.yunpicturebackend.exception.ErrorCode;
//import com.example.yunpicturebackend.modle.entity.User;
//import com.example.yunpicturebackend.modle.enums.UserRoleEnum;
//import com.example.yunpicturebackend.service.UserService;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.context.request.RequestContextHolder;
//
//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletRequest;
//
///**
// * @author Yan.z.h
// * @version 1.0
// */
//
///**
// * 利用aop实现注解权限校验
// */
//@Aspect
//@Component
//public class Auth {
//
//
//    @Resource
//    UserService userService;
//
//    @Around("@annotation(authCheck)")
//    public Object checkAuth(ProceedingJoinPoint pjp, AuthCheck authCheck) throws Throwable {
//        //获得注解值
//        String mustRole = authCheck.mustRole();
//        //从RequestContextHolder 获得当前请求
//        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
//        HttpServletRequest servletRequest = (HttpServletRequest) requestAttributes;
//        User loginUser = userService.getLoginUser(servletRequest);
//        UserRoleEnum mustRoleValue = UserRoleEnum.getEnumByValue(mustRole);
//        if (mustRoleValue == null) {
//            //放行
//            return pjp.proceed();
//        }
//        UserRoleEnum userRoleValue = UserRoleEnum.getEnumByValue(loginUser.getUserRole());
//        if (userRoleValue == null) {
//            throw new BusinessException(ErrorCode.NO_AUTH);
//        }
//        //如果需要管理员权限
//        if (UserRoleEnum.ADMIN.equals(mustRoleValue) && !UserRoleEnum.ADMIN.equals(userRoleValue)) {
//            throw new BusinessException(ErrorCode.NO_AUTH);
//        }
//        return pjp.proceed();
//
//    }
//}
